// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.ooops;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

/**
 * An implementation of {@code ClassDataReceiver} which saves received class
 * data in current directory.
 * 
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class FileClassDataReceiver implements ClassDataReceiver {

  public void acceptClassData(String className, byte[] classData) {
    try {
      FileOutputStream file = new FileOutputStream(className.replace('.', '/') + ".class");
      try {
        IOUtils.write(classData, file);
      } finally {
        file.close();
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
